<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2007 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: fastadmin: https://www.fastadmin.net/
// +----------------------------------------------------------------------
namespace app\admin\controller\order;

use app\admin\model\Adminlog as AdminlogModel;
use app\admin\model\Area;
use app\admin\model\AuthGroup as AuthGroupModel;
use app\admin\model\Car;
use app\admin\model\Staff;
use app\api\model\Order;
use app\common\controller\Adminbase;
use think\Db;
use util\Tree;

/**
 * 权限管理控制器
 */
class Index extends Adminbase
{
    protected $searchFields = 'id,status';

    protected function initialize()
    {
        parent::initialize();
        $this->modelClass = new \app\api\model\Order();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($page, $limit, $where, $sort, $order) = $this->buildTableParames();
            $list = $this->modelClass
                ->with(['car', 'area', 'staff'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $key => &$value){
                $value['timeOut'] = '-';
                $value['makeCarTime'] = '-';
                if($value['status']['status'] == 1){
                    $value['timeOut'] = time() - $value['returnTime']> 0 ?Order::secondTime(time() - $value['returnTime'] ) : '-';
                    $value['makeCarTime'] = Order::secondTime(time() - $value['getCarTime']);
                }elseif($value['status']['status'] == 2){
                    $value['timeOut'] =$value['returnCarTime'] - $value['returnTime']> 0 ?Order::secondTime($value['returnCarTime']- $value['returnTime'] ) : '-';
                    $value['makeCarTime'] = Order::secondTime($value['returnCarTime'] - $value['getCarTime']);
                }

                $value['makeTime'] = date('Y-m-d H:i:s',$value['makeTime']);
            }
            $result = ["code" => 0, 'count' => $list->total(), "data" => $list->items()];
            return json($result);
        }
        return $this->fetch();
    }


    public function edit()
    {
        $id = $this->request->param('id/d', 0);
        $data = $this->modelClass->with(['car', 'area', 'staff'])
            ->where('order.id', $id)
            ->find();

        $admin = Staff::get($data['adminId']);
        $data['admin_name'] = '';
        if($admin){
            $data['admin_name'] = $admin->name;
        }


        if($data['status']['status'] == 1){
            $data['makeCarTime'] = Order::secondTime(time() - $data['getCarTime']);
        }elseif ($data['status']['status'] ==2){
            $data['makeCarTime'] = Order::secondTime($data['returnCarTime'] - $data['getCarTime']);
        }else{
            $data['makeCarTime'] = '-';
        }

        $data['timeOut'] =$data['returnCarTime'] - $data['returnTime']> 0 ?Order::secondTime($data['returnCarTime']- $data['returnTime'] ) : '';

        $this->assign('data',$data);
        return $this->fetch();
    }


}
